import React from 'react';
import { is_empty, obj_int, loc, BtTxt, obj_str, d_sort } from 'sui';

const qbc_data = (r, setMain, mod="qbc") => {
    const ret = [];

    r.forEach(d => {
        const left = d.left;
        const right = d.right;

        const numA = left.v.length;
        const numB = right.v.length;
        const groupA = obj_str(left, "sn");
        const groupB = obj_str(right, "sn");

        const cmd = {
            india: obj_str(d, "india"),
            groupA: groupA,
            numA: numA,
            valA: left.v,
        };

        let num = Math.min(numA, numB);
        if (mod === "qbc") {
            num = Math.trunc(num / 2);
        }

        if (groupA === groupB) {
            const t = mod === "qbc" ? 4 : 2;
            num = Math.trunc(numA / t);
        } else {
            cmd["groupB"] = groupB;
            cmd["numB"] = numB;
            cmd["valB"] = right.v;
        }

        cmd["num"] = num;

        ret.push(cmd);
    });

    d_sort(ret, "india");
    ret.forEach((d, i) => d["index"] = i + 1);

    setMain(ret);
};

const qbc_task = (v, umd, mod="qbc") => {
    if (mod === "dt") {
        const dnum = obj_int(v, "dnum");
        const data = umd.cur.v;
        const val = [];

        for (let i = 0; i < dnum; i++) {
            val.push(data[i].a);
            val.push(data[i].b);
        }

        return val;
    }

    const t = mod === "qbc" ? 2 : 1;
    const dnum = obj_int(v, "dnum") * t;

    const data = umd.cur;
    const left = data.valA;
    const right = data.valB;

    const val = [];

    if (!data.groupB) {
        const n = dnum * 2;
        if (dnum > left.length) {
            return;
        }

        for (let i = 0; i < n; i++) {
            val.push(left[i]);
        }
    } else {
        if (dnum > left.length || dnum > right.length) {
            return;
        }

        for (let i = 0; i < dnum; i++) {
            val.push(left[i]);
            val.push(right[i]);
        }
    }

    return val;
};

const qbc_col = (no_acc, umd, pact, mod="qbc") => {
    const getTitle = () => {
        const tmp = mod.toUpperCase();
        return `${loc("l_892")}${tmp}${loc("l_350")}`
    };

    if (mod === "dt") {
        return [
            {name: loc("l_193"), key: 'index', width: 70},
            {name: loc("l_238"), key: 'e_opt', width: 186, formatter: (props) => {
                const d = props.row;
                return (
                    <div className="inline-flex">
                        <BtTxt hcmd={() => umd.pact(null, d, "psn")}>{loc("l_887")}</BtTxt>
                        <BtTxt hcmd={() => umd.pact(pact, d, "task")} disabled={no_acc}>{getTitle()}</BtTxt>
                    </div>
                );
            }},
            {name: `${loc("l_808")}${loc("l_807")}`, key: 'india', width: 100},
            {name: loc("l_719"), key: 'num', width: 70},
        ];
    }

    return [
        {name: loc("l_193"), key: 'index', width: 70},
        {name: loc("l_238"), key: 'e_opt', width: 186, formatter: (props) => {
            const d = props.row;
            return (
                <div className="inline-flex">
                    <BtTxt hcmd={() => umd.pact(null, d, "psn")}>{loc("l_887")}</BtTxt>
                    <BtTxt hcmd={() => umd.pact(pact, d, "task")} disabled={no_acc}>{getTitle()}</BtTxt>
                </div>
            );
        }},
        {name: `${loc("l_808")}${loc("l_807")}`, key: 'india', width: 100},
        {name: `${loc("l_748")} A`, key: 'groupA', width: 100},
        {name: `${loc("l_748")} B`, key: 'groupB', width: 100},
        {name: `${loc("l_127")} A`, key: 'numA', width: 70},
        {name: `${loc("l_127")} B`, key: 'numB', width: 70},
        {name: loc("l_719"), key: 'num', width: 70},
    ];
};

const qbc_psn = (v, cur, setMain) => {
    const val = {};
    v.forEach(d => val[d.sn] = d);

    const ret = [];
    const set = new Set();

    if (!cur.groupB) {
        let n = 1;
        const left = cur.valA.filter(d => val[d]);

        for (let i = 0; i < left.length; i++) {
            if (i + 3 >= left.length || n > cur.num) {
                break;
            }

            const a1 = val[left[i++]];
            const a2 = val[left[i++]];
            const b1 = val[left[i++]];
            const b2 = val[left[i]];

            a1["gp"] = `A${n}`;
            a2["gp"] = `A${n}`;
            b1["gp"] = `B${n}`;
            b2["gp"] = `B${n}`;

            ret.push(a1);
            ret.push(a2);
            ret.push(b1);
            ret.push(b2);

            set.add(obj_str(a1, "sn"));
            set.add(obj_str(a2, "sn"));
            set.add(obj_str(b1, "sn"));
            set.add(obj_str(b2, "sn"));

            n += 1;
        }

        left.forEach(d => {
            if (set.has(d)) {
                return;
            }

            ret.push(val[d]);
        });

        ret.forEach((d, i) => d["index"] = i + 1);
        setMain(ret);

        return;
    }

    let left = null;
    let right = null;

    if (cur.numA <= cur.numB) {
        left = cur.valA;
        right = cur.valB;
    } else {
        left = cur.valB;
        right = cur.valA;
    }

    let n = 1;

    for (let i = 0; i < left.length; i++) {
        if (i + 1 >= left.length) {
            break;
        }

        const a1 = val[left[i]];
        const a2 = val[left[i + 1]];

        a1["gp"] = `A${n}`;
        a2["gp"] = `A${n}`;

        ret.push(a1);
        ret.push(a2);

        const b1 = val[right[i]];
        const b2 = val[right[i + 1]];

        b1["gp"] = `B${n}`;
        b2["gp"] = `B${n}`;

        ret.push(b1);
        ret.push(b2);

        set.add(obj_str(a1, "sn"));
        set.add(obj_str(a2, "sn"));
        set.add(obj_str(b1, "sn"));
        set.add(obj_str(b2, "sn"));

        i += 1;
        n += 1;
    }

    left.forEach(d => {
        if (set.has(d)) {
            return;
        }

        ret.push(val[d]);
    });

    right.forEach(d => {
        if (set.has(d)) {
            return;
        }

        ret.push(val[d]);
    });

    ret.forEach((d, i) => d["index"] = i + 1);

    setMain(ret);
};

const db_psn = (v, cur, setMain) => {
    const val = {};
    v.forEach(d => val[d.sn] = d);

    const ret = [];
    const set = new Set();

    if (!cur.groupB) {
        const left = cur.valA.filter(d => val[d]);

        let n = 1;

        for (let i = 0; i < left.length; i++) {
            if (i + 1 >= left.length || n > cur.num) {
                break;
            }

            const a1 = val[left[i++]];
            const b1 = val[left[i]];

            a1["gp"] = "A";
            b1["gp"] = "B";

            ret.push(a1);
            ret.push(b1);

            set.add(obj_str(a1, "sn"));
            set.add(obj_str(b1, "sn"));

            n += 1;
        }

        left.forEach(d => {
            if (set.has(d)) {
                return;
            }

            ret.push(val[d]);
        });

        ret.forEach((d, i) => d["index"] = i + 1);
        setMain(ret);

        return;
    }

    let left = null;
    let right = null;

    if (cur.numA <= cur.numB) {
        left = cur.valA;
        right = cur.valB;
    } else {
        left = cur.valB;
        right = cur.valA;
    }

    for (let i = 0; i < left.length; i++) {
        const a1 = val[left[i]];
        a1["gp"] = "A";

        ret.push(a1);

        const b1 = val[right[i]];
        b1["gp"] = "B";

        ret.push(b1);

        set.add(obj_str(a1, "sn"));
        set.add(obj_str(b1, "sn"));
    }

    left.forEach(d => {
        if (set.has(d)) {
            return;
        }

        ret.push(val[d]);
    });

    right.forEach(d => {
        if (set.has(d)) {
            return;
        }

        ret.push(val[d]);
    });

    ret.forEach((d, i) => d["index"] = i + 1);

    setMain(ret);
};

const dt_data = (r, setMain) => {
    const ret = [];

    r.forEach(d => {
        const cmd = {
            india: obj_str(d, "india"),
            num: d.v.length,
            v: d.v,
        };

        ret.push(cmd);
    });

    d_sort(ret, "india");
    ret.forEach((d, i) => d["index"] = i + 1);

    setMain(ret);
};

const dt_psn = (v, cur, setMain) => {
    const data = cur.v;
    if (is_empty(data)) {
        setMain(null);
        return;
    }

    const val = {};
    v.forEach(d => val[d.sn] = d);

    const ret = [];

    data.forEach(d => {
        const a1 = val[d.a];
        a1["gp"] = "A";
        ret.push(a1);

        const b1 = val[d.b];
        b1["gp"] = "B";
        ret.push(b1);
    });

    ret.forEach((d, i) => d["index"] = i + 1);

    setMain(ret);
};

export {dt_psn, db_psn, qbc_psn, qbc_col, qbc_task, qbc_data, dt_data};
